package com.jwell.driverapp.ui.base

import com.jwell.cargoapp.ui.base.IBaseView
import com.jwell.panprovider.application.ConstValue
import org.reactivestreams.Subscription
import java.lang.ref.Reference
import java.lang.ref.WeakReference

/**
 * 基础presenter
 */
abstract class BasePresenter<out V : IBaseView> {
    protected var mSubscription: Subscription? = null
    private var mViewRef: Reference<V>? = null
    //刷新下标
    protected var index = 0
    //是否正在刷新
    protected var isfreshing = false
    //是否正在加载
    protected var isLoading = false
    //每页加载条数
    protected var maxResult = ConstValue.MAXVUALUE

    //取消网络请求
    open fun unSubscrib() {
        mSubscription?.cancel()
        mSubscription = null
    }

    //置空View
    open fun deattachView() {
        mViewRef?.clear()
        mViewRef = null
    }

    open fun attachView(view: Any?) {
        mViewRef = WeakReference(view as? V?)
    }

    open fun getView(): V? {
        if (mViewRef != null) {
            return mViewRef?.get()
        }
        return null
    }

    //开始时调用
    open fun start() {

    }

    //刷新，如果在加载则停止加载
    open fun fresh() {
        index = 0
    }

    //如果没有刷新，则加载
    open fun load() {

    }

    open fun onDestroy() {

    }
}